// 5011
import { isArrLike } from './isArrLike'
import { keys } from './keys'
import { optimizeCb } from './optimizeCb'
/**
 * 遍历集合中的所有元素，用每个元素当做参数调用迭代器
 * dependencies isArrLike keys optimizeCb types
 * @param {*} obj 目标集合
 * @param {*} iterator 迭代器
 * @param {*} ctx 函数上下文
 */
export function each (obj, iterator, ctx) {
  iterator = optimizeCb(iterator, ctx);
  var i, len;

  if (isArrLike(obj)) {
    for (i = 0, len = obj.length; i < len; i++) {
      iterator(obj[i], i, obj);
    }
  } else {
    var _keys = keys(obj);

    for (i = 0, len = _keys.length; i < len; i++) {
      iterator(obj[_keys[i]], _keys[i], obj);
    }
  }

  return obj;
}